package com.cysyz.web.modules.base.permission.service;

import com.cysyz.mylib.dao.Conds;
import com.cysyz.mylib.dao.PagingResult;
import com.cysyz.mylib.utils.xsvmaker.XsvMaker;
import com.cysyz.mylib.utils.xsvmaker.impl.XsvMakerImpl;
import com.cysyz.web.modules.base.permission.domain.SysPrivilege;
import com.cysyz.web.modules.base.permission.domain.SysRole;
import com.cysyz.web.modules.base.permission.domain.SysSysrolePrivilege;
import com.cysyz.web.modules.base.permission.domain.SysUserRole;
import com.cysyz.web.modules.base.support.service.BaseService;
import com.cysyz.web.modules.base.user.domain.SysUser;
import com.cysyz.web.modules.base.validate.ColumnAndValue;
import com.cysyz.web.modules.base.validate.EntityInfoValidateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiaoyujia on 2015/1/19.
 */
@Service
public class RoleService extends BaseService {

    private Integer roleUserCodeCount = 1;

    public void addPrivilegesForRole(Long roleId, List<Long> sysPrivilegeIds) {
        privilegeConfig(roleId, sysPrivilegeIds);
    }

    private void privilegeConfig(Long roleId, List<Long> sysPrivilegeIds) {
        this.m2MRelationshipMapping(SysRole.class, roleId, SysPrivilege.class, sysPrivilegeIds,SysSysrolePrivilege.class);
        List<SysPrivilege> privilegesOfRole = new ArrayList<>();

        if (sysPrivilegeIds != null && !sysPrivilegeIds.isEmpty()) {
            Conds conds = new Conds("sysPrivilege");
            conds.addFilterByDefaultTableName("id", "in", sysPrivilegeIds);
            privilegesOfRole = this.list(SysPrivilege.class, conds);

        }
        XsvMaker privileges = new XsvMakerImpl();
        for (SysPrivilege sysPrivilege : privilegesOfRole) {
            privileges.append(sysPrivilege.getCode());
        }
        SysRole sysRole = new SysRole();
        sysRole.setId(roleId);
        sysRole.setPrivileges(privileges.toString());
        this.merge(SysRole.class, sysRole, "privileges");
    }

    public void removeAllPrivilegesForRole(Long roleId) {
        this.privilegeConfig(roleId, new ArrayList<Long>());
    }

    public void addUsersForRole(Long roleId, List<String> sysUserseIds) {
        this.m2MRelationshipMapping(SysRole.class, roleId, SysUser.class, sysUserseIds, SysUserRole.class);
    }

    public void removeAllUserOfRole(Long roleId) {
        this.m2MRelationshipMapping(SysRole.class, roleId, SysUser.class, new ArrayList<Serializable>(), SysUserRole.class);
    }

    public synchronized void addNewRole(SysRole role) {
        List<SysRole> list = null;
        String code = null;
        do {
            code = "role_" + roleUserCodeCount++;

            Conds conds = new Conds();
            conds.addFilter("code", code);

            list = this.list(SysRole.class, conds);

        } while (list != null && !list.isEmpty());

        role.setCode(code);
        this.save(role);
    }

    public void editRoleInfo(SysRole sysRole) {
        this.merge(SysRole.class, sysRole, "description");
    }

    public PagingResult<SysRole> getRoleList(SysRole sysRole, int page, int pageSize) {
        Conds conds = new Conds ("SysRole");
        if (sysRole != null) {
            conds.addFilterByDefaultTableName("name", "%like%", sysRole.getName());
            conds.setOrderBy("name", "createTime", Conds.AscOrDesc.DESC);
        }
        return this.listPaging(SysRole.class, conds, page, pageSize);
    }

    @Resource
    private EntityInfoValidateService entityInfoValidateService;
    public boolean isRoleNameExists (String name) {
        return entityInfoValidateService.validateEntityExits(SysRole.class, new ColumnAndValue("name", name));
    }

    public List<SysRole> getAllRoleList() {
        return this.list(SysRole.class);
    }

}
